//
//  AppDelegate.swift
//  MyWeibo
//
//  Created by 黄勇锋 on 16/7/19.
//  Copyright © 2016年 黄勇锋. All rights reserved.
//


import UIKit
import SVProgressHUD

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //SHSQLManager.sharedTools
        
        let result = HYFStatusDAL.getCache(0, maxId: 0)
        print(result)
        
//        let result =      HYFEmoticonTools.sharedTools.getEmoticonsGroup(HYFEmoticonTools.sharedTools.defaultEmoticons)
//        print(result.count)
        
        setAppStyle()
        
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        self.window?.backgroundColor = UIColor.whiteColor()
        
        setupDefaultRootVc()
        
//        self.window?.rootViewController = UINavigationController(rootViewController: HYFComposeController())
        
        self.window?.makeKeyAndVisible()
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AppDelegate.switchRootVc(_:)), name: WBSwitchRootViewControllerNoti, object: nil)
        
        isNewVersion()
        
        return true
    }
    
    func switchRootVc(noti:NSNotification){
        
        if noti.object == nil {
            
            self.window?.rootViewController = HYFWelcomeController()
        }else {
            
         self.window?.rootViewController = HYFTabBarController()
        }
    }
    
    func setupDefaultRootVc(){
        
    //如果用户已经登录,  rootVc -> WelcomeVc
    //如果用户没有登录,  rootVc -> MainVc
        if HYFUserAccountViewModel.sharedTools.isLogin {
            
            if isNewVersion() {
                
                self.window?.rootViewController = HYFNewFeatureController()
            }else{
                //不是新版本
                self.window?.rootViewController = HYFWelcomeController()
            }
        }else{
           //没有登录
            self.window?.rootViewController = HYFTabBarController()
        }
        
    }
    
    
    //判断当前是否是最新版本
    func isNewVersion() ->Bool{
        
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        
        //将字符串转化为 double 类型
        let currentVersionD = Double(currentVersion)!
        //获取通过偏好设置保存的版本
        let hyfAppVersion = "hyfAppVersion"
        let oldVersion = NSUserDefaults.standardUserDefaults().doubleForKey(hyfAppVersion)
        
        //保存当前的版本
        NSUserDefaults.standardUserDefaults().setDouble(currentVersionD, forKey:hyfAppVersion)
        //返回是否为 最新版本
        return currentVersionD > oldVersion
    }
    
    
    func setAppStyle(){
        
//          设置 tintColor :设置控件颜色 (全局使用,必须在项目初始化之前设置)
//          UITabBar.appearance().tintColor = UIColor.orangeColor()
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        
//        SVProgressHUD.setDefaultStyle(SVProgressHUDStyle.Custom)
//        SVProgressHUD.setBackgroundColor(ThemeColor)
//        SVProgressHUD.setForegroundColor(UIColor.whiteColor())
        
    }
    
    //销毁通知
    deinit{
        
        NSNotificationCenter.defaultCenter().removeObserver(self)
        
    }
    

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
   
        
        
        
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

